package com.server.llh.llhserverwork.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.app.LelaohuirelativesApp;
import com.server.llh.llhserverwork.bean.FoodInfoBean;
import com.server.llh.llhserverwork.port.OrderFoodInterface;


public class FoodAdapter extends BaseAdapter {

	private ArrayList<FoodInfoBean> array = new ArrayList<FoodInfoBean>();
	private LayoutInflater inflater;
	OrderFoodInterface orderFoodInterface;
	Context context;
	private final static String TODAY_FOOD = "0";
	private String isScope = TODAY_FOOD;

	public FoodAdapter(Context context) {
		inflater = LayoutInflater.from(LelaohuirelativesApp.getLelaohuirelativesApp());
		this.context = context;
	}

	public void setData(ArrayList<FoodInfoBean> data, String isScope) {
		this.isScope = isScope;
		array.clear();
		array.addAll(data);
		this.notifyDataSetChanged();
	}

	public void registerCallBack(OrderFoodInterface foodInterface) {
		orderFoodInterface = foodInterface;
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public Object getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.llh_food_list_item, null);
			holder = new ViewHolder();
			convertView.setTag(holder);// 绑定ViewHolder对象
			holder.food_name = (TextView) convertView
					.findViewById(R.id.food_name);
			holder.food_price = (TextView) convertView
					.findViewById(R.id.food_price);
			holder.product_name = (TextView) convertView
					.findViewById(R.id.product_name);
			holder.food_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.food_im = (ImageView) convertView.findViewById(R.id.food_im);
			holder.food_add = (ImageView) convertView
					.findViewById(R.id.food_add);
			holder.im_reduce = (ImageView) convertView
					.findViewById(R.id.im_reduce);

		} else {
			holder = (ViewHolder) convertView.getTag();// 取出ViewHolder对象
		}
		final FoodInfoBean foodModel = array.get(position);
		holder.food_name.setText(foodModel.getProName());
		holder.food_price.setText("¥" + foodModel.getProPrice() + "元");
		holder.food_num.setText(foodModel.buyNum + "");
		holder.product_name.setText(foodModel.getSupplierName());
		holder.food_add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (isScope.equals(TODAY_FOOD)) {
					Calendar calendar = Calendar.getInstance();
					int hour = calendar.get(Calendar.HOUR_OF_DAY);// 小时
					int minute = calendar.get(Calendar.MINUTE);// 分
					if (foodModel.getMealTime()==1
							&& (hour > 6 || (hour == 6 && minute > 30))) {
						Toast.makeText(context, "已过早餐订餐时间", Toast.LENGTH_SHORT)
								.show();
						return;
					} else if (foodModel.getMealTime()==2
							&& (hour > 10 || (hour == 10 && minute > 30))) {
						Toast.makeText(context, "已过午餐订餐时间", Toast.LENGTH_SHORT)
								.show();
						return;
					} else if (foodModel.getMealTime()==3
							&& (hour > 16 || (hour == 16 && minute > 30))) {
						Toast.makeText(context, "已过晚餐订餐时间", Toast.LENGTH_SHORT)
								.show();
						return;
					}
				}
				foodModel.buyNum++;
				notifyDataSetChanged();
				if (orderFoodInterface != null) {
					orderFoodInterface.refreshShoppingList(foodModel);
				}
			}
		});
		holder.im_reduce.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				foodModel.buyNum--;
				if (foodModel.buyNum <= 0) {
					foodModel.buyNum = 0;
				}
				notifyDataSetChanged();
				if (orderFoodInterface != null) {
					orderFoodInterface.refreshShoppingList(foodModel);
				}
			}
		});
			holder.food_im.setImageResource(R.drawable.icon_launcher);

		return convertView;
	}

	static class ViewHolder {
		TextView food_name, product_name;
		TextView food_num;
		TextView food_price;
		ImageView food_im;
		ImageView food_add;
		ImageView im_reduce;

	}
}
